<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Page;

class Article extends Common
{

    public function _initialize()
    {   
        parent::_initialize();
        //分类
        $articlecate = $this->articlecate();
        $this->assign('arcate',$articlecate);
        //标签
        $label= $this->label();
        $this->assign('test',$label);
        //推荐文章 右侧导航栏
        $where['is_tui'] = ['eq',1]; //是否推荐 1 推荐
        $where['status'] = ['eq',1]; //状态是否正常 1正常
        $isarticle = db('article')->where($where)->order('create_time desc')->select();
        if($isarticle){
            foreach ($isarticle as $key => &$val) {
                $val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            }
        }
        $this->assign('isarticle',$isarticle);
        //文章详情页面 推荐随机四篇
        $isarticles = db('article')->where($where)->orderRaw('rand()')->limit(4)->order('create_time desc')->select();
        if($isarticles){
            foreach ($isarticles as $key => &$vas) {
                $vas['create_time'] = date('Y-m-d H:i:s',$vas['create_time']);
            }
        }
        $this->assign('isarticles',$isarticles);
    }

    public function index(){
        //banner
        $where['is_home'] = ['eq',2]; //资讯推荐 1
        $where['status'] = ['eq',1]; //状态 1 开启
        $banner = db('filmstrip')->field('id,img,create_time,title')->where($where)->find();
        $banner['img'] = str_replace("\\",'/',$banner['img']);
        $this->assign('banner',$banner);

        $label = input('label');
        if(isset($label) && !empty($label)){
           $label = $label;
        }else{
            $label = 0;
        }

        $this->assign('label',$label);

        $cateid = input('cateid');
        if(isset($cateid) && !empty($cateid)){
            $where['query']['cateid'] = input('cateid');
            $sewhere['cate_id'] = ['eq',$cateid];
        }
        $labelid = input('label');
        if(isset($labelid) && !empty($labelid)){
            $where['query']['label'] = input('label');
            $sewhere[] = array('exp', Db::raw("FIND_IN_SET(".$labelid.", label)"));
        }
        $sewhere['status'] = ['eq',1];
        // $where['page'] = input('param.page')?:1;
        // $where['path'] = config('url').'article/index/page/[PAGE]';
        $query = db('article')->where($sewhere)->paginate($this->pages,false,$where);
        // 获取分页显示
        $page = $query->render();
        $data = $query->all();
        foreach($data as $key=>&$val){
            $data[$key]['key'] = $key;
            $val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
            $label = explode(',', $val['label']);
            for($i=0;$i<count($label);$i++){
                $array[$i]['id'] = db('label')->where('id',$label[$i])->value('id');
                $array[$i]['name'] = db('label')->where('id',$label[$i])->value('name');
            }
            $val['labels'] = $array;
        }
        $this->assign('query',$query);
        $this->assign('page',$page);
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
    * article ajax列表
    * @param  page 页数
    * @param  limit 每页显示的条数
    * @param  where 搜索条件
    * @author 张杰
    * @time 2021 02 25
    */
    public function article_list(){
        $label = input('label');
        $cate = input('where');
        if($label != 0){
            $where[] = array('exp', Db::raw("FIND_IN_SET(".$label.", label)"));
        }
        if($cate != 0){
            $where['cate_id'] = ['eq',$cate];
        }
        $where['status'] = ['eq',1];
    	$totalItem = db('article')->where($where)->count();
		$curPage = input('curPage'); 	
        $pageSize=5;
        $totalPage =ceil($totalItem/$pageSize);  //总页数
        $startItem = ($curPage-1) * $pageSize;   //根据页码来决定查询数据的节点
    	$articlelist = db('article')->where($where)->limit($startItem,$pageSize)->select(); 
        
    	if($articlelist){
    		foreach($articlelist as &$vas){
    			$vas['create_time'] = date('Y-m-d H:i:s',$vas['create_time']);
                $label = explode(',', $vas['label']);
                for($i=0;$i<count($label);$i++){
                    $array[$i]['id'] = db('label')->where('id',$label[$i])->value('id');
                    $array[$i]['name'] = db('label')->where('id',$label[$i])->value('name');
                }
                $vas['label'] = $array;
    		}
			$result['status'] = 1;
    		$result['articlelist'] = $articlelist;
    		$result['totalItem']=$totalItem;
	        $result['pageSize']=$pageSize;
	        $result['totalPage']=$totalPage;
    	}else{
            $result['status'] = 0;
            $result['articlelist'] = "";
        }
    	return json($result);
    }


    public function detail(){
        $id = input('id');
        if(!empty($id)){
            $where['id'] = ['eq',$id];
            $detail = db('article')->where($where)->find();
            if($detail){
                $detail['create_time'] = date('Y-m-d H:i:s',$detail['create_time']);
                db('article')->where($where)->setInc('views');
                $this->assign('artdetail',$detail);
                return $this->fetch();
            }else{
                //跳转页面404;
                header("Location:/index/nohtml");
            }
        }else{
            //跳转页面404
            header("Location:/index/nohtml");
        }
    	
    }

}
